home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ShareWare OnLine 2
/
ShareWare OnLine Volume 2 (CMS Software)(1993).iso
/
games2
/
rotise12.zip
/
RDB_PLAY.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-04-03
|
9KB
|
372 lines
/* RDB_PLAYR - RDB player creating subroutines */
#ifdef __TURBOC__
#include <stdlib.h>
#endif
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#ifdef __TURBOC__
#include <alloc.h>
#else
#include <malloc.h>
#endif
#include "rotise.h"
/* External data referenced */
extern CMDLIST CmdTable[]; /* cmdtable.c */
extern int Cmd; /* rotise.c */
/* Local data publicly available */
int SlotC; /* Number of slots defined */
int SlotS; /* Size of slot table */
RDB_SLOT *Slottbl; /* Slot table */
/* Private data */
/* Table of position/mask pairs */
static struct {
char *pos;
UWORD posmask;
char *desc;
} Postbl[] = {
{ "P", POS_P, "Pitcher" }, /* Pitcher */
{ "C", POS_C, "Catcher" }, /* Catcher */
{ "1B", POS_1B, "First Base" }, /* First */
{ "2B", POS_2B, "Second Base" }, /* Second */
{ "3B", POS_3B, "Third Base" }, /* Third */
{ "SS", POS_SS, "Shortstop" }, /* Short */
{ "OF", POS_OF, "Outfield" }, /* Outfield */
{ "DH", POS_DH, "Designated Hitter" }, /* DH */
{ NULL, 0 }
};
/* Local Prototypes */
void build_player PROTO( (RDB_PLAYER *rp, int tokcnt, char *tokens[]) );
BOOL verify_contract PROTO( (char *) );
BOOL verify_salary PROTO( (char *) );
/*
BOOL rdb_add_player( RDB_PLAYER *rp, int tokcnt, char *tokens[] )
Verify the data and then add to the player structure.
ACCEPTS:
RDB_PLAYER *rp - player structure to fill in
int tokcnt - how many tokens on this line
char *tokens[] - array pointing to each token
RETURNS:
BOOL <value> - TRUE if everything was okay, FALSE if things really aren't
*/
BOOL rdb_add_player ARGLIST( (rp, tokcnt, tokens) )
NFARG( RDB_PLAYER *rp )
NFARG( int tokcnt )
FARG( char *tokens[] )
{
int slotN;
if ( tokcnt < (ARG_ADD_NAME+1) )
{ /* minimum number of arguments for a player */
rdb_error( "Add ", CmdTable[Cmd].usage );
return FALSE;
}
if ( !verify_salary( tokens[ARG_ADD_SAL] ) )
{ /* salary doesn't pass muster, complain */
rdb_error( "Add - Incomprehensible salary: ", tokens[ARG_ADD_SAL] );
return FALSE;
}
if ( !verify_contract( tokens[ARG_ADD_CONT] ) )
{ /* unknown contract identifier */
rdb_error( "Add - Unknown contract: ", tokens[ARG_ADD_CONT] );
return FALSE;
}
slotN = slot_find( tokens[ARG_ADD_SLOT] );
if ( slotN < 0 )
{ /* slot no good */
rdb_error( "Add - Invalid Slot.", tokens[ARG_ADD_SLOT] );
return FALSE;
}
if ( strlen( tokens[ARG_ADD_PK1] ) > sizeof( rp->pname.pn_lname )-1 )
{ /* last name of key too big */
rdb_error( "Add - Player key, part 1, too long.", tokens[ARG_ADD_PK1] );
return FALSE;
}
if ( strlen( tokens[ARG_ADD_PK2] ) > sizeof( rp->pname.pn_fname )-1 )
{ /* last name of key too big */
rdb_error( "Add - Player key, part 2, too long.", tokens[ARG_ADD_PK2] );
return FALSE;
}
/* okay, we have all the verified data ready to go into the structure */
build_player( rp, tokcnt, tokens );
return TRUE;
}
/*
BOOL build_player( RDB_PLAYER *rp, int tokcnt, char *tokens[] )
Actually put the player data into passed structure. Should be all verified
by now.
ACCEPTS:
RDB_PLAYER *rp - player structure to fill in
int tokcnt - how many tokens on this line
char *tokens[] - array pointing to each token
RETURNS:
BOOL <value> - TRUE if everything was okay, FALSE if things really aren't
*/
void build_player ARGLIST( (new_player, tokcnt, tokens) )
NFARG( RDB_PLAYER *new_player )
NFARG( int tokcnt )
FARG( char *tokens[] )
{
int kk;
int slotN;
/* first, build the players name */
for ( kk = ARG_ADD_NAME; kk < tokcnt; kk++ )
{ /* run the player name together, seperated by spaces */
if ( (strlen( tokens[kk] ) + strlen( new_player->name )) > MAX_NAMESIZE )
{ /* can't fit any more, complain and truncate */
rdb_error( "Build - Player name too long, truncating.", tokens[kk] );
break;
}
else if ( kk != ARG_ADD_NAME )
{ /* put in a space */
strcat( new_player->name, " " );
}
strcat( new_player->name, tokens[kk] );
}
/* copy in strings */
strcpy( new_player->pname.pn_lname, strupr( tokens[ARG_ADD_PK1] ) );
strcpy( new_player->pname.pn_fname, strupr( tokens[ARG_ADD_PK2] ));
slotN = slot_find( tokens[ARG_ADD_SLOT] );
new_player->slot = slotN;
if ( slotN < 0 )
{ /* Unknown slot -- give warning and continue. */
rdb_error( "Build - unknown slot: ", tokens[ARG_ADD_SLOT] );
}
new_player->status = STATUS_ACTIVE; /* Show activeness */
new_player->salary = atoi( tokens[ARG_ADD_SAL] );
switch( tolower(*tokens[ARG_ADD_CONT]) )
{ /* get contract value */
case 'a': /* first year */
new_player->contract = CONT_1;
break;
case 'b': /* second year */
new_player->contract = CONT_2;
break;
case 'o': /* option year */
new_player->contract = CONT_OPT;
break;
default: /* long term - say how many years left */
new_player->contract = CONT_YR;
new_player->contract |= ( atoi( tokens[ARG_ADD_CONT] ) << CONT_YRSH );
break;
}
new_player->next = NULL;
}
/* verify_salary - make sure the passed salary is reasonable, i.e., it's
/* all numbers and not zero. */
BOOL verify_salary ARGLIST( (salary) )
FARG( char *salary )
{
int kk;
int sal_len = strlen( salary );
for ( kk = 0; kk < sal_len; kk++ )
{ /* scan the string for non-number */
if ( !isdigit( *(salary+kk) ) )
{ /* oops, problem with salary */
return FALSE;
}
}
if ( atoi( salary ) == 0 )
{ /* must not be zero, okay? */
return FALSE;
}
return TRUE;
}
/* verify_contract - make sure contract is reasonable */
static char *Contracts = "abo123456789";
BOOL verify_contract ARGLIST( (contract) )
FARG( char *contract )
{
if ( strlen( contract ) > 1 )
{ /* can't be longer than one */
return FALSE;
}
if ( strchr( Contracts, *contract ) == NULL )
{ /* not found in contract */
return FALSE;
}
return TRUE;
}
char *contract_string ARGLIST( (contract) )
FARG( int contract )
{
char *x;
switch( contract )
{ /* get contract value */
case CONT_1: /* first year */
return "A";
case CONT_2: /* second year */
return "B";
case CONT_OPT: /* option year */
return "O";
default: /* long term - say how many years left */
x = "x";
*x = '0' + (contract >> CONT_YRSH);
return x;
}
}
/* Return slot data given its slot number */
RDB_SLOT *slot_get ARGLIST( (slotnum) )
FARG( int slotnum )
{
if ( ( slotnum < 0 ) || ( slotnum >= SlotC ) )
{ /* Slot number is out of range */
return ( ( RDB_SLOT * ) NULL );
}
return( &Slottbl[slotnum] );
}
/* Return slot number given its slot name */
int slot_find ARGLIST( (slotname) )
FARG( char *slotname )
{
int slotnum;
/* Make sure name is not too long */
if ( strlen( slotname ) > MAX_SLOT )
return ( -1 );
/* Go through the table */
for( slotnum = 0; slotnum < SlotC; ++slotnum )
if ( stricmp( slotname, Slottbl[slotnum].slot ) == 0 )
return ( slotnum );
/* Didn't find one */
return ( -1 );
}
/* Add a new slot */
BOOL slot_add ARGLIST( (slotP) )
FARG( RDB_SLOT *slotP )
{
int tblsize; /* Size required for new table */
/* Make sure this slot name has not already been used */
if ( slot_find( slotP->slot ) >= 0 )
return ( FALSE );
/* Add an entry in the table */
if ( SlotC == SlotS )
{ /* Must reallocate a table */
tblsize = (SlotS+10) * sizeof( RDB_SLOT );
if ( SlotS == 0 )
Slottbl = (RDB_SLOT *)malloc( tblsize );
else
Slottbl = (RDB_SLOT *)realloc( Slottbl, tblsize );
if ( Slottbl == NULL )
{
rdb_error( "Can't allocate slots table.", "" );
return ( FALSE );
}
SlotS += 10;
}
/* Fill in new entry */
strcpy( Slottbl[SlotC].slot, slotP->slot );
Slottbl[SlotC].pos = slotP->pos;
return (SlotC++);
}
/* Convert position name to position mask */
UWORD pos_to_mask ARGLIST( (pos) )
FARG( char *pos )
{
int i;
for ( i = 0; Postbl[i].pos != NULL; ++i )
{ /* Lookup the position */
if ( stricmp( pos, Postbl[i].pos ) == 0 )
return ( Postbl[i].posmask );
}
/* Not found. */
return ( 0 );
}
/* Convert position mask to position name */
char *mask_to_pos ARGLIST( (posmask) )
FARG( UWORD posmask )
{
int i;
for ( i = 0; Postbl[i].pos != NULL; ++i )
{ /* Lookup the mask */
if ( posmask == Postbl[i].posmask )
return ( Postbl[i].pos );
}
/* Not found. */
return ( (char *)NULL );
}
/* Convert position mask to position description */
char *mask_to_posdesc ARGLIST( (posmask) )
FARG( UWORD posmask )
{
int i;
for ( i = 0; Postbl[i].pos != NULL; ++i )
{ /* Lookup the mask */
if ( posmask == Postbl[i].posmask )
return ( Postbl[i].desc );
}
/* Not found. */
return ( (char *)NULL );
}